package com.kvest.gpsservicetracker.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.kvest.gpsservicetracker.service.GPSTrackerService;
import com.kvest.gpsservicetracker.utils.LoggerService;

public class ProviderChangedReceiver extends BroadcastReceiver {
    private static final String TAG = "KVEST_TAG";

    @Override
    public void onReceive(Context context, Intent intent) {
        //Если extras == null, то это наш случай. Полученно эксперементальным путем
        if (intent.getExtras() == null) {
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            LoggerService.getInstance().writeLog("ProviderChanged[GPS_PROVIDER=" + locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) +
                    ", NETWORK_PROVIDER=" + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) + "]");
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
                locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                GPSTrackerService.startTracking(context);
            } else {
                LoggerService.getInstance().writeLog("All providers are switched off");
                GPSTrackerService.stopTracking(context);
            }
        }
    }
}
